package com.hos.check.common.util;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class ModelUtil {

	/**
	 * 判断一个对象是否被Hibernate的延时加载机制给修改，并返回正确的类名
	 * 
	 * @param mainModel
	 * @return
	 */
	public static String getModelClassName(Object mainModel) {
		String rtnVal = mainModel.getClass().getName();
		int i = rtnVal.indexOf('$');
		if (i > -1)
			rtnVal = rtnVal.substring(0, i);
		return rtnVal;
	}


	/**
	 * 对象克隆
	 * 
	 * @param object
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws InvocationTargetException
	 */
	public static Object clone(Object object) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException,
			InvocationTargetException {
		String clsName = getModelClassName(object);
		Object newObj = Class.forName(clsName).newInstance();
		BeanUtils.copyProperties(newObj, object);
		return newObj;
	}

}
